ListNode* sortList(ListNode* head) {
        // 自底向上归并排序
        if (!head) return head;

        // 计算链表长度
        int length = 0;
        ListNode *node = head;
        while(node)
        {
            length++;
            node = node->next;
        }
        // 自底向上归并主循环
        ListNode *dummy = new ListNode(0, head);
        for(int subLength = 1; subLength < length; subLength <<= 1) // subLength从1开始倍增（1->2->4...）
        {
            ListNode *pre = dummy, *cur = dummy->next;
            while(cur)
            {
                // 分割两个子链表
                ListNode *head1 = cur;
                for(int i = 1; i < subLength && cur->next; ++i)
                {
                    cur = cur->next;
                }

                ListNode *head2 = cur->next;
                cur->next = nullptr;
                cur = head2;

                for(int i = 1; i < subLength && cur && cur->next; ++i)
                {
                    cur = cur->next;
                }

                ListNode *next = nullptr;
                if(cur)
                {
                    next = cur->next;
                    cur->next = nullptr;
                }
                ListNode *merged = merge(head1, head2);

                pre->next = merged; // 将合并后的链表连接到已排序部分
                while(pre->next)
                {
                    pre = pre->next; // 移动 pre 到合并链表的末尾
                }
                cur = next; // 处理后续未合并的节点
            }
        }
        return dummy->next;
    }
    ListNode *merge(ListNode *head1, ListNode *head2) // 合并两个有序子链表
    {
        ListNode *dummy = new ListNode(0);
        ListNode *tmp = dummy, *t1 = head1, *t2 = head2;
        while(t1 && t2)
        {
            if(t1->val <= t2->val)
            {
                tmp->next = t1;
                t1 = t1->next;
            }
            else
            {
                tmp->next = t2;
                t2 = t2->next;
            }
            tmp = tmp->next;
        }
        if(t1) tmp->next = t1;
        else if(t2) tmp->next = t2;

        return dummy->next;
    }